package com.drools.project;

import com.drools.project.model.complex.Event;
import com.drools.project.model.complex.Student;
import com.drools.project.model.coupon.Order;
import com.drools.project.model.discount.OrderDiscount;
import com.drools.project.model.discount.OrderRequest;
import com.drools.project.model.coupon.User;
import com.drools.project.ruleService.ComplexService;
import com.drools.project.ruleService.CouponService;
import com.drools.project.ruleService.OrderDiscountService;
import jakarta.annotation.Resource;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@SpringBootApplication
public class DroolsProjectApplication implements CommandLineRunner {

    @Resource
    private CouponService couponService;

    @Resource
    private OrderDiscountService orderDiscountService;

    @Resource
    private ComplexService complexService;


    public static void main(String[] args) {
        SpringApplication.run(DroolsProjectApplication.class, args);
    }

    @Override
    public void run(String... args) {
        System.out.println("=======================规则案例一--基本用法规则==============================");
        Calendar calendar = Calendar.getInstance();
        calendar.add(Calendar.YEAR, -1);
        Date oneYearAgo = calendar.getTime();

        Order order1 = new Order();
        order1.setCategory("Electronics");
        order1.setAmount(300);
        order1.setOrderDate(oneYearAgo);

        Order order2 = new Order();
        order2.setCategory("Books");
        order2.setAmount(200);
        order2.setOrderDate(oneYearAgo);

        Order order3 = new Order();
        order3.setCategory("Electronics");
        order3.setAmount(600);
        order3.setOrderDate(new Date());

        List<Order> orders = new ArrayList<>();
        orders.add(order1);
        orders.add(order2);
        orders.add(order3);

        User user = new User();
        user.setName("John Doe");
        user.setVip(true);
        user.setPurchaseCount(15);
        user.setTotalPurchaseAmount(2000);
        user.setOrders(orders);
        user.setCouponAmount(0);
        user.setLocation("Shanghai");
        user.setPoints(1500);
        couponService.applyCoupons(user);

        System.out.println("当前用户总优惠券金额为: " + user.getCouponAmount());

        System.out.println("=======================规则案例二--global规则==============================");

        OrderRequest orderRequest = new OrderRequest();
        orderRequest.setCustomerNumber("123456");
        orderRequest.setAge(55);
        orderRequest.setAmount(2000);
        orderRequest.setCustomerType(com.drools.project.enums.CustomerType.LOYAL);

        OrderDiscount orderDiscount = orderDiscountService.getDiscount(orderRequest);
        System.out.println("获得的折扣：" + orderDiscount.getDiscount());

        //===========================================================================================
        List<Student> studentList = new ArrayList<>();
        studentList.add(new Student("John",  20, 90,"Shanghai"));
        studentList.add(new Student("Jane",  25, 85,"Beijing"));
        studentList.add(new Student("Jim",   17, 95,"Shanghai"));
        studentList.add(new Student("Judy",  35, 80,"Beijing"));
        studentList.add(new Student("Jason", 15, 90,"Shanghai"));
        studentList.add(new Student("Julia", 45, 85,"Beijing"));
        studentList.add(new Student("James", 19, 76,"Shanghai"));
        studentList.add(new Student("Julie", 16, 80,"Beijing"));

        System.out.println("=======================规则案例三--query函数==============================");
        complexService.complexQuery(studentList);
        System.out.println("=======================规则案例四--function函数==============================");
        complexService.complexFunction(studentList);
        System.out.println("=======================规则案例五--priority规则==============================");
        complexService.complexPriority(studentList);
        System.out.println("=======================规则案例六--role,flow,group规则==============================");
        complexService.complexRoleFlowGroup();
        System.out.println("=======================规则案例七--event规则==============================");
        List<Event> eventList = new ArrayList<>();
        long timestamp = System.currentTimeMillis();
        eventList.add(new Event("LOGIN", timestamp-50000));
        eventList.add(new Event("LOGIN", timestamp-40000));
        eventList.add(new Event("LOGIN", timestamp-80000));
        eventList.add(new Event("LOGOUT", timestamp-30000));
        complexService.complexEvent(eventList);
    }
}
